<template>
    <DialogSubmitForm ref="dialogSubmitForm" class="add-activity" v-bind="form"></DialogSubmitForm>
</template>
<script>
import DialogSubmitForm from '@/components/SubmitForm/DialogSubmitForm.vue'
import { addActivity, isActivityNameExist } from '@/api/market/activity'
import { mapGetters } from 'vuex'
const SAVE_SUCCESS = 'saveSuccess'
export default {
    components: {
        DialogSubmitForm
    },
    emits: [SAVE_SUCCESS],
    computed: {
        ...mapGetters(['name'])
    },
    data() {
        return {
            form: {
                dialogTitle: '添加活动',
                width: '750px',
                submitFormFrame: [
                    {
                        inline: true,
                        colon: false,
                        items: [
                            [
                                {
                                    label: '活动名称',
                                    key: 'activityName',
                                    frameType: 'input',
                                    placeholder: '请输入活动名称',
                                    tooltip: '中英文数字和下划线 ,最大长度30',
                                    validations: [
                                        {
                                            required: true,
                                            message: '请输入活动名称',
                                            trigger: 'blur'
                                        },
                                        {
                                            max: 30,
                                            message: '活动名称不能超过30个字符',
                                            trigger: 'blur'
                                        },
                                        {
                                            trigger: 'blur',
                                            validator(rule, value, callback) {
                                                let error = new Error('活动名称只能包含中英文数字和下划线')
                                                var pattern = /^[a-zA-Z0-9_\u4e00-\u9fa5]*$/
                                                var result = pattern.test(value)
                                                return result ? callback() : callback(error)
                                            }
                                        },
                                        {
                                            trigger: 'blur',
                                            validator(rule, value, callback) {
                                                let error = new Error('该活动名称已存在,请重新填写!')
                                                isActivityNameExist({
                                                    activityName: value
                                                })
                                                    .then((data) => {
                                                        if (data.data > 0) {
                                                            return callback(error)
                                                        }
                                                        callback()
                                                    })
                                                    .catch((data) => callback(data.message))
                                            }
                                        }
                                    ]
                                },
                                {
                                    label: '类型',
                                    key: 'type',
                                    frameType: 'select',
                                    options: [
                                        {
                                            id: 1,
                                            text: '满减'
                                        }
                                    ],
                                    value: 1,
                                    validations: {
                                        required: true
                                    }
                                }
                            ],
                            [
                                {
                                    label: '开始时间',
                                    key: 'startTime',
                                    frameType: 'datetime',
                                    validations: {
                                        required: true
                                    }
                                },
                                {
                                    label: '结束时间',
                                    key: 'endTime',
                                    frameType: 'datetime',
                                    validations: {
                                        required: true
                                    }
                                }
                            ],

                            [
                                {
                                    label: '请上传文件',
                                    key: 'file',
                                    placeholder: '请选择',
                                    frameType: 'upload',
                                    tplHref: '/价格模板.xlsx',
                                    tplTitle: '模板下载',
                                    accept: '.xlsx',
                                    validations: {
                                        required: '请上传商品文件'
                                    }
                                }
                            ]
                        ]
                    }
                ],
                submitFun: this.add,
                labelWidth: '100px'
            }
        }
    },
    methods: {
        add(data) {
            //开始时间
            var startTime = data.startTime
            if (startTime == '') {
                this.$message.error('开始时间不能为空!')
                return false
            }
            //校验开始时间必须大于当前时间
            if (new Date(startTime).getTime() <= new Date().getTime()) {
                this.$message.error('开始时间必须要大于当前时间！')
                return false
            }
            //结束时间
            var endTime = data.endTime
            if (endTime == '') {
                this.$message.error('结束时间不能为空!')
                return false
            }
            //校验结束时间必须大于开始时间
            if (new Date(endTime).getTime() <= new Date(startTime).getTime()) {
                this.$message.error('结束时间必须要大于开始时间！')
                return false
            }

            var formdata = {
                activityName: data.activityName,
                type: data.type,
                excel: data.file[0].raw,
                userName: this.name,
                startTime: data.startTime,
                endTime: data.endTime,
                sendTime: data.sendTime
            }
            return addActivity(formdata).then(() => {
                this.$message.success('添加活动成功！')
                this.$refs.dialogSubmitForm.hide()
                this.$emit(SAVE_SUCCESS)
            })
        },
        show() {
            this.$refs.dialogSubmitForm.show()
        }
    }
}
</script>
<style lang="scss"></style>
